package cn.wolfcode.shop.seckillserver.service.impl;

import cn.wolfcode.shop.common.exception.BusinessException;
import cn.wolfcode.shop.common.util.CodeMsg;
import cn.wolfcode.shop.util.BaseKeyPrefix;
import cn.wolfcode.shop.redis.RedisService;
import cn.wolfcode.shop.seckillapi.domain.OrderInfo;
import cn.wolfcode.shop.seckillapi.domain.SeckillOrder;
import cn.wolfcode.shop.seckillapi.service.IOrderInfoService;
import cn.wolfcode.shop.seckillapi.service.ISeckillGoodService;
import cn.wolfcode.shop.seckillapi.service.ISeckillOrderService;
import cn.wolfcode.shop.seckillapi.vo.SeckillOrderVO;
import cn.wolfcode.shop.seckillserver.mapper.SeckillOrderMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SeckillOrderServiceImpl implements ISeckillOrderService {

    @Autowired
    private ISeckillGoodService seckillGoodService;

    @Autowired
    private IOrderInfoService orderInfoService;

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;

    @Autowired
    private RedisService redisService;



    /**
     * 秒杀订单生成
     * @param seckillOrderVO
     * @param id
     * @return
     */
    @Override
    public String doSeckill(SeckillOrderVO seckillOrderVO, Long id) {

        //创建Redis 订单信息:用来做订单重复查询
        BaseKeyPrefix sokey = new BaseKeyPrefix(0, "SECKILL_ORDER:");
        redisService.set(sokey,seckillOrderVO.getGoodId()+":"+ id,"成功");

        //删减Mysql库存
        seckillGoodService.reduce(seckillOrderVO.getGoodId());

        //创建Mysql商品订单
        OrderInfo orderInfo = orderInfoService.createOrderInfo(seckillOrderVO,id);

        //创建Mysql秒杀订单
        SeckillOrder seckillOrder = new SeckillOrder();
        BeanUtils.copyProperties(orderInfo,seckillOrder);
        seckillOrderMapper.createSeckillOrder(seckillOrder);

        //订单号
        return orderInfo.getOrderNo();
    }

    /**
     * 根据订单号 查询订单信息
     * @param orderNo
     * @return
     */
    @Override
    public OrderInfo findByOrderNo(String orderNo) {
        return seckillOrderMapper.findByOrderNo(orderNo);
    }

    /**
     * 根据商品ID 回滚数据
     * 查询MySQL数据库真实数据,作为Redis 回滚数据依据
     * @param goodId
     */
    @Override
    public void syncStock(Long goodId) {

        BaseKeyPrefix stockKey = new BaseKeyPrefix(0, "SECKILL_GOOD_STOCK");

        redisService.set(stockKey,goodId+"",seckillGoodService.findByGoodId(goodId).getStockCount());

    }

    /**
     * 延迟消息队列,对超时订单的处理业务
     * @param orderNo
     * @param goodId
     */
    @Override
    public void timeOutCancelOrder(String orderNo, Long goodId) {

        //更改数据库订单状态
        int status = seckillGoodService.timeOutCancelOrder(orderNo);

        if (status != 0){
            //更新商品库存
            seckillGoodService.updateByGoodId(goodId);
            //回滚redis数据
            syncStock(goodId);
            //抛订单超时异常
            throw new BusinessException(CodeMsg.ORDERING_ERROR);
        }

    }

    @Override
    public boolean updatePayState(String orderNo, Integer statusAccountPaid) {
        int i = seckillOrderMapper.updatePayState(orderNo, statusAccountPaid);
        if (i == 0){
            return false;
        }
        return true;
    }

    /**
     *
     */
    @Override
    public void orderInit() {

        List<SeckillOrder> seckillOrders = seckillOrderMapper.list();

        BaseKeyPrefix key = new BaseKeyPrefix(0, "SECKILL_ORDER:");
        for (SeckillOrder seckillOrder : seckillOrders) {
            redisService.set(key,seckillOrder.getGoodId()+":"+seckillOrder.getUserId(),seckillOrder);
        }
    }
}
